package com.springcloud.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spingcloud.travel.common.CommonConst;
import com.spingcloud.travel.common.TableResult;
import com.springcloud.travel.entity.Order;
import com.springcloud.travel.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * @author fyb
 * @version 1.0
 * @date 2021/3/24 9:49
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * @param order
     * @return java.lang.Boolean
     * @Author fyb
     * @Description //TODO 新增订单
     * @Date 10:57 2021/3/24
     **/
    @PostMapping("/addOrder")
    public Boolean addOrder(@RequestBody Order order) {
        try {
            boolean result = orderService.save(order);

            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * @param order
     * @return java.lang.Boolean
     * @Author fyb
     * @Description //TODO 修改订单数据
     * @Date 10:58 2021/3/24
     **/
    @PostMapping("/updateOrder")
    public Boolean updateOrder(@RequestBody Order order) {
        try {
            boolean result = orderService.updateById(order);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * @Author fyb
     * @Description //TODO 批量删除订单
     * @Date 11:00 2021/3/24
     * @param ids
     * @return java.lang.Boolean
     **/
    @PostMapping("/delOrder")
    public Boolean delOrder(@RequestParam("ids") String[] ids) {
        try {
            boolean result = orderService.removeByIds(Arrays.asList(ids));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

   /**
    * @Author fyb
    * @Description //TODO   //获取订单列表
    * @Date 11:10 2021/3/24
    * @param map
    * @return com.spingcloud.travel.common.TableResult<com.springcloud.travel.entity.Order>
    **/
    @GetMapping("/getOrder")
    public TableResult<Order> getOrder(@RequestParam Map<String, Object> map){
        try {
            String page = map.get("page").toString();
            String limit = map.get("limit").toString();
            //当页码为null，进行初始化
            if (StringUtils.isEmpty(page)){
                page = CommonConst.PAGE;
            }
            //当条数为null，进行初始化
            if (StringUtils.isEmpty(limit)){
                limit = CommonConst.SIZE;
            }
            Page<Order> pages = new Page<>();

            pages.setCurrent(Integer.parseInt(page));
            pages.setSize(Integer.parseInt(limit));
            //分页查询
            Page<Order> orderPage = orderService.page(pages,this.getQueryWrapper(map));

            //封装结果集
            return TableResult.getSuccess((int)orderPage.getTotal(), orderPage.getRecords());
        }catch (Exception e){
            e.printStackTrace();
        }
        return TableResult.getFailWithData("查询失败",0,null);
    }

    /**
     * @Author fyb
     * @Description //TODO  分页加条件查询
     * @Date 16:37 2021/3/25
     * @param map
     * @return com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<com.springcloud.travel.entity.Order>
     **/
    private QueryWrapper<Order> getQueryWrapper(Map<String,Object> map){
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        String orderName = (String) map.get("orderName");
        String orderState = (String) map.get("orderState");
        // 判断是否为空
        if (!StringUtils.isEmpty(orderName)){
            queryWrapper.like("order_name",orderName);
        }
        if (!StringUtils.isEmpty(orderState)){
            queryWrapper.like("order_state",orderState);
        }
        return queryWrapper;
    }

}
